package com.app.shop.service.system.impl;

import com.app.shop.util.Constant;
import com.app.shop.util.DictModel;
import com.app.shop.dao.system.SysDictMapper;
import com.app.shop.entity.SysDict;
import com.app.shop.entity.SysDictExample;
import com.app.shop.service.system.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 初始化系统字典
 *
 * @author chenliwei
 * @version v1.0 2019-3-11
 */
@Service("dictService")
public class DictServiceImp implements DictService {

    private static final Logger log = LoggerFactory.getLogger(DictServiceImp.class);

    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public boolean initSysDict() {

        boolean bool=true;
        try {
            //查询所有有效字典
            SysDictExample sysDictExample=new SysDictExample();
            sysDictExample.createCriteria().andStatusEqualTo(1);
            //根据所有key将所有码值补充到字典中
            List<SysDict> list=sysDictMapper.selectByExample(sysDictExample);
            //独立获取所有key
            for(SysDict sysDict:list){
                if(!Constant.DIC_MAP.containsKey(sysDict.getKeyCode())){
                    Constant.DIC_MAP.put(sysDict.getKeyCode(), new ArrayList<DictModel>());
                }
                Constant.DIC_MAP.get(sysDict.getKeyCode()).add(new DictModel(sysDict));
            }
        } catch (Exception e) {
            log.error("初始化字典异常！！！"+e);
            bool=false;
            e.printStackTrace();
        }

        return bool;
    }
}
